package com.example.schoolenterprisealliance.fragment;

//首页
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.schoolenterprisealliance.R;
import com.example.schoolenterprisealliance.activity.ItemActivity;
import com.example.schoolenterprisealliance.adapter.Hister_enventAdapter;
import com.example.schoolenterprisealliance.adapter.InformationAdapter;
import com.example.schoolenterprisealliance.bean.History_event;
import com.example.schoolenterprisealliance.bean.Information;
import com.example.schoolenterprisealliance.html.HtmlText;
import com.example.schoolenterprisealliance.io.AppConfig;
import com.example.schoolenterprisealliance.io.GlideImageLoader;
import com.example.schoolenterprisealliance.service.MainService;
import com.example.schoolenterprisealliance.time.Time;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;

import java.util.ArrayList;
import java.util.List;


public class HomePageFragment extends Fragment {
    private View view;
    private InformationAdapter informationAdapter;
    private Hister_enventAdapter hister_enventAdapter;
    private RecyclerView IrecyclerView;//资讯表列表
    private RecyclerView HrecycleView;//历史事件表
    private Banner banner;
    private int i=0;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_home_page, container, false);

        IrecyclerView=view.findViewById(R.id.rv_information);
        HrecycleView=view.findViewById(R.id.rv_history_event);
        banner =  view.findViewById(R.id.banner);
        
        IrecyclerView.setOverScrollMode(View.OVER_SCROLL_NEVER);
        HrecycleView.setOverScrollMode(View.OVER_SCROLL_NEVER);
        setBanner();
        layoutManager();
        informationData();
        historyData();
        return view;
    }
    //设置布局管理器
    private void layoutManager(){
        LinearLayoutManager linearLayoutManager= new LinearLayoutManager(getActivity());
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        IrecyclerView.setLayoutManager(linearLayoutManager);
        LinearLayoutManager linearLayoutManager1=new LinearLayoutManager(getActivity());
        linearLayoutManager1.setOrientation(LinearLayoutManager.HORIZONTAL);
        HrecycleView.setLayoutManager(linearLayoutManager1);
    }
    //轮播图
    private void setBanner(){
        //1, 声明Retrofit 的构建者
        Retrofit.Builder builder = new Retrofit.Builder();
        //2, 设置公共的地址
        builder.baseUrl(AppConfig.BASE_URL);
        //3, 设置解析gson
        builder.addConverterFactory(GsonConverterFactory.create());
        //4, 得到Retrofit 对象
        Retrofit retrofit = builder.build();
        //5, 得到接口
        MainService mainService = retrofit.create(MainService.class);
        //6, 得到Call 的方法
        Call<Information[]> call = mainService.getInformationData();
        //7, 传入接口回调 , 获取数据
        call.enqueue(new Callback<Information[]>() {
            @Override
            public void onResponse(Call<Information[]> call, Response<Information[]> response) {
                //获得数据
                Information[] information = response.body();
                List<String> imageUrlList=new ArrayList<>();
                for (int i = 0; i < information.length; i++) {
                    imageUrlList.add(information[i].getImgUrl());
                    Log.e("information",information[i].getImgUrl());
                }
                //设置图片集合
                banner.setImages(imageUrlList);
                //设置banner样式
                banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
                //设置图片加载器
                banner.setImageLoader(new GlideImageLoader());
                //设置banner动画效果
                banner.setBannerAnimation(Transformer.DepthPage);
                //设置自动轮播，默认为true
                banner.isAutoPlay(true);
                //设置轮播时间
                banner.setDelayTime(1500);
                //设置指示器位置（当banner模式中有指示器时）
                banner.setIndicatorGravity(BannerConfig.CENTER);
                //banner设置方法全部调用完毕时最后调用
                banner.start();
            }
            @Override
            public void onFailure(Call<Information[]> call, Throwable t) {
                Log.e("error", t.getMessage());
                t.printStackTrace();
            }
        });
    }
    //获取最新资讯数据
    private void informationData() {
        //1, 声明Retrofit 的构建者
        Retrofit.Builder builder = new Retrofit.Builder();
        //2, 设置公共的地址
        builder.baseUrl(AppConfig.BASE_URL);
        //3, 设置解析gson
        builder.addConverterFactory(GsonConverterFactory.create());
        //4, 得到Retrofit 对象
        Retrofit retrofit = builder.build();
        //5, 得到接口
        MainService mainService = retrofit.create(MainService.class);
        //6, 得到Call 的方法
        Call<Information[]> call = mainService.getInformationData();
        //7, 传入接口回调 , 获取数据
        call.enqueue(new Callback<Information[]>() {
            @Override
            public void onResponse(Call<Information[]> call, Response<Information[]> response) {
                //获得数据
                Information[] information = response.body();
                List<Information> informationList=new ArrayList<>();
                Information information1;
                for (int i = 0; i < information.length; i++) {
                    information1=new Information();
                    information1.setTitle(information[i].getTitle());
                    information1.setContext(HtmlText.Html.delHTMLTag(information[i].getContext()));
                    information1.setImgUrl(information[i].getImgUrl());
                    information1.setTime(information[i].getTime());
                    informationList.add(information1);
                }
                Log.e("iv_left",informationList.size()+"");
                informationAdapter =new InformationAdapter(R.layout.item_main_information,informationList);
                IrecyclerView.setAdapter(informationAdapter);
                informationAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                    @Override
                    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                        Intent intent=new Intent(getActivity(), ItemActivity.class);
                        intent.putExtra("title",informationList.get(position).getTitle());
                        intent.putExtra("time", Time.longToString(informationList.get(position).getTime()));
                        intent.putExtra("context", HtmlText.Html.delHTMLTag(informationList.get(position).getContext()));

                        startActivity(intent);
                    }
                });
                Log.e("response", information.getClass().toString());
            }
            @Override
            public void onFailure(Call<Information[]> call, Throwable t) {
                Log.e("error", t.getMessage());
                t.printStackTrace();
            }


        });
    }
    //获取历史数据
    private void historyData() {
        //1, 声明Retrofit 的构建者
        Retrofit.Builder builder = new Retrofit.Builder();
        //2, 设置公共的地址
        builder.baseUrl(AppConfig.BASE_URL);
        //3, 设置解析gson
        builder.addConverterFactory(GsonConverterFactory.create());
        //4, 得到Retrofit 对象
        Retrofit retrofit = builder.build();
        //5, 得到接口
        MainService mainService = retrofit.create(MainService.class);
        //6, 得到Call 的方法
        Call<History_event[]> call = mainService.getHistory_eventData();
        //7, 传入接口回调 , 获取数据
        call.enqueue(new Callback<History_event[]>() {
            @Override
            public void onResponse(Call<History_event[]> call, Response<History_event[]> response) {
                History_event[] history_events=response.body();
                List<History_event> history_eventList=new ArrayList<>();
                History_event history_event;
                for(int i=0;i<history_events.length;i++){
                    history_event=new History_event();
                    history_event.setImgUrl(history_events[i].getImgUrl());
                    history_event.setTitle(history_events[i].getTitle());
                    history_event.setContext(HtmlText.Html.delHTMLTag(history_events[i].getContext()));
                    history_event.setTime(history_events[i].getTime());
                    Log.e("history_events",history_events.toString());
                    history_eventList.add(history_event);
                }
                hister_enventAdapter=new Hister_enventAdapter(R.layout.item_main_information,history_eventList);
                hister_enventAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                    @Override
                    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                        Intent intent=new Intent(getActivity(), ItemActivity.class);
                        intent.putExtra("title",history_eventList.get(position).getTitle());
                        intent.putExtra("time",history_eventList.get(position).getTime());
                        intent.putExtra("context", HtmlText.Html.delHTMLTag(history_eventList.get(position).getContext()));
                        startActivity(intent);
                    }
                });
                HrecycleView.setAdapter(hister_enventAdapter);
                Log.e("history_events_response", history_events.getClass().toString());
            }

            @Override
            public void onFailure(Call<History_event[]> call, Throwable t) {
                Log.e("History_eventerror", t.getMessage());
                t.printStackTrace();
            }
        });
    }


}
